package _6_reflect_proxy;

import _0_object_oriented.User;
import org.junit.Test;

import javax.annotation.Nullable;
import java.lang.reflect.Field;

/**
 * Created by bpzj on 2019-4-2.
 */
public class ReflectDemo {

    @Test
    public void getClassObject() throws ClassNotFoundException {
        // 第一种方法
        Class cls1 = String.class;

        // 第二种方法 Object 的 getClass 方法
        String a = "123";
        Class cls2 = a.getClass();

        // 第三种, 常用, 性能高
        Class cls3 = Class.forName("java.lang.String");
    }

    @Test
    public void methodOfClassObj() throws ClassNotFoundException {
        Class cls = String.class;
        System.out.println(cls.getName());
    }

    @Test
    public void fieldAndFieldValue() throws ClassNotFoundException, IllegalAccessException {
        User user = new User();
        user.setName("minmin");
        user.setAge(26);
        user.setHeight(1);
        Class<?> clazz = User.class;
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object value = field.get(user);
            if (value != null) {
                System.out.println(field.getName() + ": " + value.toString());
            }
            if (field.isAnnotationPresent(Nullable.class)) {
                System.out.println("测试注解方法");
            }
        }
    }

}
